//购物车控制层
app.controller('cartController', function ($scope, cartService) {
    //查询购物车列表
    $scope.findCartList = function () {
        console.log("order1:" + $scope.order.sellerId);
        cartService.findCartList().success(
            function (response) {
                $scope.cartList = response;
                console.log("cartList:" + $scope.cartList);
                $scope.totalValue = cartService.sum($scope.cartList);
                console.log("totalValue:" + $scope.totalValue);
                console.log("order2:" + $scope.order.sellerId);
            }
        );
    };

    $scope.checkPrice = function (price) {
        console.log("price:" + price);
        var b = (price <= 0);
        console.log("price b:" + b);
        if (b) {
            alert("购物车里空空如也");
        } else {
            window.location.href = '../order/getOrderInfo';
        }
    };

    $scope.deleteObjectd = function (itemId, num) {
        console.log("itemId:" + itemId + ",num:" + -num);
        var numr = -num;
        cartService.addGoodsToCartList(itemId, numr).success(
            function (response) {
                if (response.success) {//如果成功
                    $scope.findCartList();//刷新列表
                } else {
                    alert(response.message);
                }
            }
        );
    };

    //数量加减
    $scope.addGoodsToCartList = function (itemId, num) {
        cartService.addGoodsToCartList(itemId, num).success(
            function (response) {
                if (response.success) {//如果成功
                    $scope.findCartList();//刷新列表
                } else {
                    alert(response.message);
                }
            }
        );
    };

    //获取当前用户的地址列表
    $scope.findAddressList = function () {
        cartService.findAddressList().success(
            function (response) {
                $scope.addressList = response;
                for (var i = 0; i < $scope.addressList.length; i++) {
                    if ($scope.addressList[i].isDefault == '1') {
                        $scope.address = $scope.addressList[i];
                        break;
                    }
                }

            }
        );
    };

    //选择地址
    $scope.selectAddress = function (address) {
        $scope.address = address;
    }

    //判断某地址对象是不是当前选择的地址
    $scope.isSeletedAddress = function (address) {
        if (address == $scope.address) {
            return true;
        } else {
            return false;
        }
    }

    $scope.order = {paymentType: '1'};//订单对象

    //选择支付类型
    $scope.selectPayType = function (type) {
        $scope.order.paymentType = type;
    }

    //保存订单
    $scope.submitOrder = function () {
        $scope.order.receiverAreaName = $scope.address.address;//地址
        $scope.order.receiverMobile = $scope.address.mobile;//手机
        $scope.order.receiver = $scope.address.contact;//联系人
        console.log("order3:" + $scope.order.sellerId);
        var payment = $scope.totalValue.totalMoney.toFixed(2);
        console.log("order3 payment:" + payment);
        cartService.submitOrder($scope.order).success(
            function (response) {
                //alert(response.message);
                if (response.success) {
                    //页面跳转
                    if ($scope.order.paymentType == '1') {
                        //如果是微信支付，跳转到支付页面
                        location.href = "../pay/success?payment=" + payment;
                        // location.href = "pay.html";
                    } else {
                        //如果货到付款，跳转到提示页面
                        location.href = "../pay/success?payment=" + payment;
                    }

                } else {
                    alert(response);	//也可以跳转到提示页面
                }

            }
        );
    }

});